package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.FlowTemplate_YeWuZLDao;
import net.mysoft.budgetsoft.system.domain.TFlowTemplate;
import net.mysoft.budgetsoft.system.domain.TFlowTemplate_YeWuZL;
import net.mysoft.budgetsoft.system.pagemodel.FlowTemplate_YeWuZL;
import net.mysoft.budgetsoft.system.service.FlowTemplate_YeWuZLService;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2013-07-23 11:37:31 
 */
@Service("flowTemplate_YeWuZLService")
public class FlowTemplate_YeWuZLServiceImpl implements FlowTemplate_YeWuZLService{
	@Resource
	private FlowTemplate_YeWuZLDao flowTemplate_YeWuZLDao;
	
	public FlowTemplate_YeWuZLDao getFlowTemplate_YeWuZLDao(){
		return this.flowTemplate_YeWuZLDao;
	}
	
	public void setFlowTemplate_YeWuZLDao(FlowTemplate_YeWuZLDao flowTemplate_YeWuZLDao){
		this.flowTemplate_YeWuZLDao = flowTemplate_YeWuZLDao;
	}
	
	@Override
	public void saveList(FlowTemplate_YeWuZL flowTemplate_YeWuZL){
		String ftl_IDs = flowTemplate_YeWuZL.getFtl_IDs();
	//	flowTemplate_YeWuZLDao.executeHql("delete TFlowTemplate_YeWuZL t where t.yeWuZLBH = ?",new String[]{flowTemplate_YeWuZL.getYeWuZLBH()});
		List list = flowTemplate_YeWuZLDao.find("from TFlowTemplate_YeWuZL t where t.yeWuZLBH = ?",new String[]{flowTemplate_YeWuZL.getYeWuZLBH()});
		flowTemplate_YeWuZLDao.delete(list);
		if(ftl_IDs!=null&&!"".equals(ftl_IDs)){
			String[] ftl_IDArr = ftl_IDs.split(",");
			for(String ftl_ID:ftl_IDArr){
				TFlowTemplate_YeWuZL t = new TFlowTemplate_YeWuZL();
				t.setFtl_ID(ftl_ID);
				t.setYeWuZLBH(flowTemplate_YeWuZL.getYeWuZLBH());
				flowTemplate_YeWuZLDao.save(t);
			}
		}
	}
	
	@Override
	public void add(FlowTemplate_YeWuZL flowTemplate_YeWuZL){
		TFlowTemplate_YeWuZL t = new TFlowTemplate_YeWuZL();
		BeanUtils.copyProperties(flowTemplate_YeWuZL, t);
		//t.setFlowTemplate_YeWuZL_ID(UUID.randomUUID().toString());
		flowTemplate_YeWuZLDao.save(t);
	}
	
	@Override
	public void edit(FlowTemplate_YeWuZL flowTemplate_YeWuZL){
		TFlowTemplate_YeWuZL t = new TFlowTemplate_YeWuZL();
		BeanUtils.copyProperties(flowTemplate_YeWuZL, t);
		flowTemplate_YeWuZLDao.update(t);
	}
	
	@Override
	public void delete(FlowTemplate_YeWuZL flowTemplate_YeWuZL) {
		for (String id : flowTemplate_YeWuZL.getIds().split(",")) {
			Object t =  flowTemplate_YeWuZLDao.get(TFlowTemplate_YeWuZL.class, id);
			if (t != null) {
				 flowTemplate_YeWuZLDao.delete(t);
			}
		}
	}
	
	@Override
	public DataGrid datagrid(FlowTemplate_YeWuZL flowTemplate_YeWuZL){
		DataGrid dg = new DataGrid();
		String hql = "from TFlowTemplate_YeWuZL t where 1=1 ";
		List params = new ArrayList();
		hql = addWhere(flowTemplate_YeWuZL, hql, params);
		hql = addOrder(flowTemplate_YeWuZL, hql);
		String totalHql = "select count(*) " + hql;
		List<TFlowTemplate_YeWuZL> toList = flowTemplate_YeWuZLDao.find(hql, params, flowTemplate_YeWuZL.getPage(), flowTemplate_YeWuZL.getRows());
		List<FlowTemplate_YeWuZL> oList = new ArrayList<FlowTemplate_YeWuZL>();
		oList = changeModel(toList);
		dg.setTotal(flowTemplate_YeWuZLDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}
	
	private String addWhere(FlowTemplate_YeWuZL flowTemplate_YeWuZL, String hql, List<Object> params) {
		if(flowTemplate_YeWuZL.getYeWuZLBH()!=null&&!"".equals(flowTemplate_YeWuZL.getYeWuZLBH())){;
			hql += " and t.yeWuZLBH = ?";
			params.add(flowTemplate_YeWuZL.getYeWuZLBH());
		}
		return hql;
	}
	
	private String addOrder(FlowTemplate_YeWuZL flowTemplate_YeWuZL, String hql) {
		if (flowTemplate_YeWuZL.getSort() != null) {
			hql += " order by " + flowTemplate_YeWuZL.getSort() + " " + flowTemplate_YeWuZL.getOrder();
		}
		return hql;
	}
	
	public List<FlowTemplate_YeWuZL> changeModel(List<TFlowTemplate_YeWuZL> tFlowTemplate_YeWuZL ){
		List<FlowTemplate_YeWuZL> list = new ArrayList<FlowTemplate_YeWuZL>();
		for(TFlowTemplate_YeWuZL to:tFlowTemplate_YeWuZL){
			if(to.getFtl_ID()!=null&&!"".equals(to.getFtl_ID().trim())){
				TFlowTemplate tft = (TFlowTemplate) flowTemplate_YeWuZLDao.get(TFlowTemplate.class, to.getFtl_ID());
				if(tft!=null){
					FlowTemplate_YeWuZL o = new FlowTemplate_YeWuZL();
					BeanUtils.copyProperties(to, o);
					o.setFtl_Name_cn(tft.getFtl_Name_cn());
					o.setFtl_Name_en(tft.getFtl_Name_en());
					list.add(o);
					o.setFtc_ID(tft.getFtc_ID());
				}
			}
		}
		return list;
	}

	@Override
	public List setting(FlowTemplate_YeWuZL flowTemplate_YeWuZL) {
		/*String hql = "from TFlowTemplate_YeWuZL t where 1=1 ";
		List params = new ArrayList();
		hql = addWhere(flowTemplate_YeWuZL, hql, params);
		hql = addOrder(flowTemplate_YeWuZL, hql);
		List<TFlowTemplate_YeWuZL> toList = flowTemplate_YeWuZLDao.find(hql, params);
		List<FlowTemplate_YeWuZL> oList = new ArrayList<FlowTemplate_YeWuZL>();
		oList = changeModel(toList);
		List<TFlowTemplateClass> list = flowTemplate_YeWuZLDao.find("from TFlowTemplateClass", new ArrayList());

		for(TFlowTemplateClass tfc:list){
			boolean temp = false;
			for(FlowTemplate_YeWuZL o: oList){////System.out.println(o.getFtc_ID()+"@@@"+tfc.getFtc_ID());
				if(o.getFtc_ID()!=null&&tfc.getFtc_ID().equals(o.getFtc_ID())){
					o.setFtc_Name_cn(tfc.getFtc_Name_cn());
					o.setFtc_Name_en(tfc.getFtc_Name_en());
					temp = true;
					break;
				}
			}
			if(!temp){
				FlowTemplate_YeWuZL o= new FlowTemplate_YeWuZL();
				o.setFtc_Name_cn(tfc.getFtc_Name_cn());
				o.setFtc_Name_en(tfc.getFtc_Name_en());
				o.setFtc_ID(tfc.getFtc_ID());
				oList.add(o);
			}
		}
		return oList;*/
		return null;
	}
}
